import lombok.Data;
import org.assertj.core.util.Lists;

import java.math.BigDecimal;
import java.util.List;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2020/10/22
 * @since 5.0
 */
public class MapUtil {
    // 地球半径
    private static final double EARTH_RADIUS = 6370996.81;

    // 弧度
    private static double radian(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * @param lat1
     * @param lat2
     * @param lng2
     * @return void
     * @pram lng1
     * @Description: 计算两经纬度距离多少米
     */
    public static double distanceOfTwoPoints(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = radian(lat1);
        double radLat2 = radian(lat2);
        double a = radLat1 - radLat2;
        double b = radian(lng1) - radian(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        double ss = s * 1.0936132983377;
        System.out.println("两点间的距离是：" + s + "米" + "," + (int) ss + "码");
        return s;
    }

    public static void main(String[] args) {
        //欢乐谷
        double lat1 = 22.541395;
        double lng1 = 113.980392;
        List<BmapPoint> pointList = Lists.newArrayList();
        BmapPoint p1 = new BmapPoint(lng1, lat1);
        // 世界之窗
        double lat2 = 22.534576;
        double lng2 = 113.973015;
        BmapPoint p2 = new BmapPoint(lng2, lat2);
        distanceOfTwoPoints(lat1, lng1, lat2, lng2);
        //机场
        double lat3 = 22.631834;
        double lng3 = 113.816208;
        BmapPoint p3 = new BmapPoint(lng3, lat3);
        distanceOfTwoPoints(lat1, lng1, lat3, lng3);
        //凤凰山
        double lat4 = 22.67429200000;
        double lng4 = 113.850937;
        distanceOfTwoPoints(lat1, lng1, lat4, lng4);
        BmapPoint p4 = new BmapPoint(lng4, lat4);
        //莲花山
        double lat5 = 22.54711;
        double lng5 = 114.059502;
        double d = 1123.1111232323232;
        distanceOfTwoPoints(lat1, lng1, lat5, lng5);
        BmapPoint p5 = new BmapPoint(lng5, lat5);
        pointList.add(p2);
        pointList.add(p1);
        pointList.add(p3);
        pointList.add(p4);
        pointList.add(p5);
        BigDecimal min = null;
        BmapPoint minpoint = null;
        BigDecimal distince = new BigDecimal(1000);
        for (BmapPoint bmapPoint : pointList) {
            double v = distanceOfTwoPoints(lat1, lng1, bmapPoint.getLat(), bmapPoint.getLng());
            BigDecimal distinceDecimal = new BigDecimal(v);
            if (distince.compareTo(distinceDecimal) >= 0) {
                if (min == null) {
                    min = distinceDecimal;
                    minpoint =bmapPoint;
                } else {
                    if (min.compareTo(distinceDecimal) >= 0) {
                        min = distinceDecimal;
                        minpoint =bmapPoint;
                    }
                }
            }
        }
        System.out.println("min :" +minpoint);
    }

    @Data
    public static class BmapPoint {
        private double lng;// 经度

        private double lat;// 纬度

        public BmapPoint(double lng, double lat) {
            this.lng = lng;
            this.lat = lat;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof BmapPoint) {
                BmapPoint bmapPoint = (BmapPoint) obj;
                return (bmapPoint.getLng() == lng && bmapPoint.getLat() == lat) ? true : false;
            } else {
                return false;
            }
        }
    }
}

    